package com.party.product.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.party.domain.Site;
import com.party.domain.SiteCategory;
import com.party.dto.Page;
import com.party.exception.ExceptionMsgEnum;
import com.party.exception.ServiceException;
import com.party.product.mapper.SiteCategoryMapper;
import com.party.product.mapper.SiteMapper;
import com.party.product.service.SiteCategoryService;
/**
 * @author Caizhf
 * @date 2017年8月31日上午11:51:52
 * @version v.0.1
 * @email 1115054416@qq.com 
 *
 * <p>Description: </p>
 *
 */
@Service
public class SiteCategoryServiceImpl implements SiteCategoryService{
	@Autowired
	private SiteCategoryMapper mapper;
	@Autowired
	private SiteMapper siteMapper;
	
	
	@Override
	@Transactional(readOnly=true)
	public List<SiteCategory> list(int pageNum, int pageSize) {
		int totalRecord = mapper.countList();
		Page page = new Page(totalRecord,pageNum,pageSize);
		List<Integer> ids = mapper.preparedPage(page.getStartindex(),pageSize);
		if(null==ids||ids.size()==0)
			ids.add(0);
		return mapper.list(ids);
	}

	@Override
	public SiteCategory findById(int soid) {
		return mapper.findById(soid);
	}

	@Override
	@Transactional(readOnly=true)
	public List<Site> listSiteByCategoryId(int pageNum, int pageSize, int soid) {
		int totalRecord = siteMapper.countListByCategoryId(soid);
		Page page = new Page(totalRecord,pageNum,pageSize);
		List<Integer> ids = siteMapper.preparedPage(page.getStartindex(),pageSize,soid);
		if(null==ids||ids.size()==0)
			ids.add(0);
		return siteMapper.listByIds(ids);
	}

	@Override
	public void save(SiteCategory siteCategory) {
		if(mapper.save(siteCategory)==null)
			throw new ServiceException(ExceptionMsgEnum.SERVER_SAVE_ERROR);
	}

	@Override
	public void update(SiteCategory siteCategory) {
		if(mapper.update(siteCategory)==null)
			throw new ServiceException(ExceptionMsgEnum.SERVER_UPDATE_ERROR);
	}

	@Override
	@Transactional
	public void delete(int soid) {
		List<Integer> ids = mapper.listAllSiteIdsByCategoryId(soid);
		if(null!=ids&&ids.size()!=0)
			mapper.removeCategoryForeignKeyByIds(ids);
		if(mapper.deleteById(soid)==null)
			throw new ServiceException(ExceptionMsgEnum.SERVER_DELETE_ERROR);
	}

	@Override
	public void updateSequence(int soid, int sequence) {
		if(mapper.updateSequenceById(soid,sequence)==null)
			throw new ServiceException(ExceptionMsgEnum.SERVER_UPDATE_ERROR);
	}
	
}
